package com.carpedil.client.service.impl;

import com.carpedil.client.entity.ClientOrder;
import com.carpedil.client.movie.service.FeiginMovieService;
import com.carpedil.client.order.service.FeiginOrderService;
import com.carpedil.client.service.ClientOrderService;
import com.carpedil.common.dto.MovieDTO;
import com.carpedil.common.dto.OrderDTO;
import com.carpedil.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/** @author Administrator */
@Service
public class ClientService {
  @Autowired ClientOrderService clientOrderService;
  @Autowired FeiginOrderService feiginOrderService;
  @Autowired FeiginMovieService feiginMovieService;

  public String booking(Integer movieId) {
    MovieDTO movie = findMovieById(movieId);
    // 生成订单编号
    String orderId = StringUtil.createOrderId();
    // 保存客户端订单
    OrderDTO order = genericOrder(orderId, movie.getMovieName());
    // 将订单号发送至订单系统
    return feiginOrderService.createOrder(order);
  }

  private MovieDTO findMovieById(Integer movieId) {
    return feiginMovieService.findMovieById(movieId);
  }

  private OrderDTO genericOrder(String orderId, String movieName) {
    ClientOrder clientOrder = new ClientOrder();
    clientOrder.setOrderId(orderId);
    clientOrder.setMovieName(movieName);
    clientOrder.setStatus(0);
    clientOrderService.save(clientOrder);
    OrderDTO dto = new OrderDTO();
    BeanUtils.copyProperties(clientOrder, dto);
    return dto;
  }

  public List<MovieDTO> getAllMovie() {
    return feiginMovieService.getAllMovie();
  }
}
